let messageQueue = [];

let sendMessage = (message) => {
  messageQueue.push(message);
  if (messageQueue.length === 1) {
    queueMicrotask(() => {
      const json = JSON.stringify(messageQueue);
      messageQueue.length === 0;
      console.log(json);
    });
  }
};

queueMicrotask(() => {
  console.log('queueMicrotask');
});

sendMessage('test1');
sendMessage('test2');
sendMessage('test3');
